package com.focus.xyz.union.pages.frontend;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by chenmeng on 2016/5/9.
 */
public class UnionPayPage extends BasePage {
    private Action action;
    private static final String url = "http://union.xyz.cn/unionPay/pay";
    private static final String url1 = "http://payment-test.chinapay.com/pay/SnetPin.jsp";

    @FindBy(xpath = "//*[@id=\"chinaUnionpay\"]/img")
    private WebElement unionpayLink;

    @FindBy(name = "CardId")
    private WebElement cardId;
    @FindBy(name = "Pin")
    private WebElement pin;
    @FindBy(xpath = "//input[@value='1'][@type='radio']")
    private WebElement type;

    @FindBy(name = "ExtraCode")
    private WebElement extracode;
    @FindBy(name = "cmdOk")
    private WebElement cmdOk;
    @FindBy(xpath = "//div[@class='pro_mid']/table/tbody/tr[1]/td")
    private WebElement orderId;
    /*支付页的立即跳转链接*/
    @FindBy(xpath = "//img[@src='/banksel/images/zjtz.gif']")
    private WebElement immediateForwardLink;

    @FindBy(xpath = "//a[contains(text(),'查看订单详情')]")
    private WebElement orderInfoLink;

    /*充值账户支付*/
    @FindBy(id = "payPwd")
    private WebElement payPwd;
    @FindBy(id = "agreeToPay")
    private WebElement agreeToPay;
    @FindBy(xpath = "//form[@id=\"form_accpay\"]/table/tbody/tr[1]/td[1]")
    private WebElement payPrice;
    @FindBy(xpath = "//form[@id=\"form_accpay\"]/table/tbody/tr[2]/td")
    private WebElement balance;
    @FindBy(xpath = "//div[@class='pt10 pay-detail-text']")
    private WebElement totalPrice;



    public UnionPayPage(WebDriver driver, Action action) {
        super(driver, action);
        this.action = action;
        PageFactory.initElements(driver,this);
//        switchTo(url);
    }

    public void clickUnionPayLink() throws Exception{
        action.click(unionpayLink);
    }

    public void inputAcctPayPass(String payPass) throws Exception{
        action.inputText(this.payPwd,payPass);
    }

    public void clickAcctPayBtn() throws Exception{
        action.click(agreeToPay);
    }

    public void selectType() throws Exception{
        action.click(type);
    }

    public void inputCardNo(String cardNo) throws Exception{
        action.inputText(cardId,cardNo);
    }

    public void inputPin(String pinNo) throws Exception{
        action.inputText(pin,pinNo);
    }
    public void submit() throws Exception{
        action.click(cmdOk);
    }

    /*
    * 银联账号支付
    * */
    public void unionpay() throws Exception{
        switchTo(url1);
        inputCardNo("123456789001");
        selectType();
        inputPin("789001");
        inputExtraCode("1234");
        submit();
        action.waitForPageLoad();//等待支付页加载完成
        linkImmediateForward();//点击立即跳转
        action.pause(3000l);
    }

    /*
    * 充值账户支付
    * */
    public void acctPay(String paypass) throws Exception{
        inputAcctPayPass(paypass);
        clickAcctPayBtn();
    }

    public String getPayPrice() throws Exception{
        return payPrice.getText();
    }

    public String getTotalPrice() throws Exception{
        String text = action.getText(By.xpath("//div[@class='pt10 pay-detail-text']"));
        return text.substring(text.indexOf("¥") + 1,text.indexOf("查看详情")).trim();
    }

    public void inputExtraCode(String ec) throws Exception{
        action.inputText(extracode,ec);
    }

    public void linkOrderInfo() throws Exception{
        action.click(orderInfoLink);
    }

    public void linkImmediateForward() throws Exception{
        action.click(immediateForwardLink);
    }

    public String getOrderId() throws Exception{
        String order = orderId.getText().trim();
        order = StringUtils.substringAfter(order,"：");
        return order;
    }
}
